package com.example.floristshop;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.floristshop.adapters.SelectFlowerAdapter;
import com.example.floristshop.database.Flower;

import org.litepal.LitePal;

import java.util.List;

public class RecordSelectFlower extends AppCompatActivity {

    Toolbar toolbar;
    ListView listView;

    SelectFlowerAdapter adapter;

    List<Flower> flowerList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_select_flower);

        // init data
        flowerList = LitePal.findAll(Flower.class);

        // get view
        toolbar = (Toolbar)findViewById(R.id.select_flower_toolbar);
        listView = (ListView)findViewById(R.id.select_flower_listview);

        // set view
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {//toolbar的返回按钮
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });

        adapter = new SelectFlowerAdapter(RecordSelectFlower.this, R.layout.flower_item, flowerList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Flower flower = flowerList.get(position);
                Intent intent = new Intent();
                intent.putExtra("flower_id", flower.getFlower_id());
                setResult(RESULT_OK, intent);
                finish();
            }
        });

    }
}